package org.csu.mypetstore_seller.service;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore_seller.domain.Category;
import org.csu.mypetstore_seller.domain.Item;
import org.csu.mypetstore_seller.domain.Product;
import org.csu.mypetstore_seller.persistence.CategoryMapper;
import org.csu.mypetstore_seller.persistence.ItemMapper;
import org.csu.mypetstore_seller.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ItemMapper itemMapper;

    public List<Product> getProductList(String Id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("category", Id);
        return productMapper.selectList(queryWrapper);
    }

    public void insertProduct(Product product) {
        productMapper.insert(product);
    }

    public void deleteProductByName(String name) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("name", name);
        productMapper.delete(queryWrapper);
    }

    public Product getProductByproductId(String productId) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        return (Product)productMapper.selectList(queryWrapper).get(0);
    }

    public void updateProduct(Product product, String productId){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productId", productId);
        productMapper.update(product, queryWrapper);
    }

    // 获取总产品数量
    public Integer getNum(){
        return itemMapper.selectList(null).size();
    }


    // 获取缺货商品数量（库存<=0）
    public Integer getNum1(){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("amount", 0);// le=less or equal
        return itemMapper.selectList(queryWrapper).size();
    }
}
